using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using SigmaStock_Components;

namespace project
{
    public partial class Pay_Retailer : Form
    {
        OleDbConnection conn;
        OleDbCommand command;
        string query;
        int amount_paid;
        int curr_total;

        public static int amt = 0;

        public int curr_amt
        {
            set
            {
                amt = value;
            }
        }

        public Pay_Retailer()
        {
            query = "";
            InitializeComponent();
            dbconnection();
            int amount_due;
            RNameText.Text = Form3.retailer_purchaselist[0].RNAME.ToString();
            query = "select AMOUNT_DUE from retailer_credit where RNAME='" + Form3.retailer_purchaselist[0].RNAME + "';";
            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                curr_total = int.Parse(reader[0].ToString());
            }
            int total = amt + curr_total;
            textBox2.Text = total.ToString();
        }

        private void dbconnection()
        {
            try
            {
                conn = DBComponent.Mag_DealerDB.dbconnection();
                conn.Open();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }

        private void Pay_Button_Click(object sender, EventArgs e)
        {
            if (!(String.IsNullOrEmpty(PayAmountText.Text)) && Validation.IsPositiveNumber(PayAmountText.Text))
            {
                int amount_due = int.Parse(textBox2.Text);
                amount_paid = int.Parse(PayAmountText.Text);
                Console.WriteLine("Amount due : " + amount_due);
                Console.WriteLine("Amount paid : " + amount_paid);
                if (amount_paid <=amount_due )
                {
                    SaveRetailerReceipt();
                    Payment_Retailer.UpdateRetailerSold();
                    Payment_Retailer.UpdateMagazineSold();
                    UpdateTransactions();
                    UpdateRetailerCredit();
                    Payment_Retailer.UpdateStockDate();
                    //     UpdateOrderMagazine();
                    Program.InvokeModal(this, "project.Retailer_Receipt");                    
                }
                else
                {
                    MessageBox.Show("Payment amount must be less than amount due !!!");
                    PayAmountText.ResetText();
                }
                
            }
        }

        private void SaveRetailerReceipt()
        {
            Retailer_Receipt_Object ret_rec = new Retailer_Receipt_Object();
            ret_rec.amount_paid = int.Parse(PayAmountText.Text);
            ret_rec.current_due = int.Parse(textBox2.Text) - ret_rec.amount_paid;
            ret_rec.rname = RNameText.Text;
            ret_rec.trans_date = DateTime.Now;
            Form3.ret_receipt = ret_rec;
        }

        private void UpdateTransactions()
        {
            //try
            //{
            string from = RNameText.Text;//"KNA";
            string transaction_type = "Retailer_Payment";
            int amount = int.Parse(PayAmountText.Text);
            string to = "KNA";
            query = "insert into transaction1(CURR_DATE,TRANSACTION_TYPE,TRANS_FROM,TRANS_TO,AMOUNT) VALUES('" + DateTime.Now + "','" + transaction_type + "','" + from + "','" + to + "','" + amount + "');";
            command = new OleDbCommand(query, conn);
            command.ExecuteNonQuery();
            //}
            /*catch (Exception e)
            {
                Console.WriteLine(e);
            }*/
        }
        private void UpdateRetailerCredit()
        {
            int credit_amt = 0;
            int amount_due = 0;
            query = "select CREDIT_AMOUNT,AMOUNT_DUE from retailer_credit where RNAME='" + Form3.retailer_purchaselist[0].RNAME + "';";
            command.CommandText = query;
            command.Connection = conn;
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                credit_amt = int.Parse(reader[0].ToString());
                amount_due = int.Parse(reader[1].ToString());
            }
            amount_due += amt;
            amount_due -= amount_paid;
            reader.Close();
            query = "update retailer_credit set AMOUNT_DUE='" + amount_due + "' where RNAME='"+Form3.retailer_purchaselist[0].RNAME+"';";
            command.CommandText = query;
            command.Connection = conn;
            command.ExecuteNonQuery();
        }

        private void Pay_Reatiler_Load(object sender, EventArgs e)
        {

        }

        private void Pay_Reatiler_FormClosed(object sender, FormClosedEventArgs e)
        {
                        Form3.DataReset();
            if (Application.OpenForms.Count == 1)
            {
                Program.main_form.Show();
            }
        }
        }


}